<template>
  <div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" :destroy-on-close="true" :append-to-body="true" :close-on-click-modal="false">
      <el-form :model="sysTestUserForm" :rules="sysTestUserRules" ref="sysTestUserForm">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="jobKey">
            <el-input v-model="sysTestUserForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth" prop="jobKey">
            <el-input v-model="sysTestUserForm.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="爱好" :label-width="formLabelWidth" prop="jobKey">
            <el-input v-model="sysTestUserForm.hobby" autocomplete="off"></el-input>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addOrEditSysTestUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {Message} from "element-ui";
  import {addSysTestUser,editSysTestUser, getSysTestUsers} from "@/api/sysTestUser";
  export default {
    name: "SysTestUserDialog",
    data() {
      return {
        formLabelWidth: '120px',
          sysTestUserRules: {
              name: [
                { required: true, message: '请输入姓名', trigger: 'blur' }
              ],
              age: [
                { required: true, message: '请输入年龄', trigger: 'blur' }
              ],
              hobby: [
                { required: true, message: '请输入爱好', trigger: 'blur' }
              ],
        }
      }
    },
    computed: {
      dialogFormVisible: {
        get() {
          return this.$store.state.sysTestUser.dialogFormVisible;
        },
        set(value) {
          this.$store.state.sysTestUser.dialogFormVisible = value;
        }
      },
      dialogTitle: {
        get() {
          return this.$store.state.sysTestUser.dialogTitle;
        },
        set(value) {
          this.$store.state.sysTestUser.dialogTitle = value;
        }

      },
        sysTestUserForm: {
          get() {
            return this.$store.state.sysTestUser.sysTestUserForm;
          },
          set(value) {
            this.$store.state.sysTestUser.sysTestUserForm = value;
          }
      }
    },
    methods: {
      closeDialog() {
        this.$store.state.sysTestUser.dialogFormVisible = false;
      },
      addOrEditSysTestUser() {
        this.$refs["sysTestUserForm"].validate((valid) => {
          if (valid) {
            if (this.dialogTitle === "添加测试用户") {
              addSysTestUser(this.sysTestUserForm).then((res) => {
                Message.success(res.data.msg);
                this.sysTestUserForm = {};
                this.dialogFormVisible = false;
                var params = {
                  "current": this.$store.state.sysTestUser.currentPage,
                  "size": this.$store.state.sysTestUser.pageSize
                };
                Message.success(res.data.msg);
                // 刷新
                getSysTestUsers(params).then((res) => {
                  this.$store.state.sysTestUser.tableData = res.data.data;
                  this.$store.state.sysTestUser.total = res.data.total;
                })
              })
            } else {
              editSysTestUser(this.sysTestUserForm).then((res) => {
                Message.success(res.data.msg);
                this.sysTestUserForm = {};
                this.dialogFormVisible = false;
                var params = {
                  "current": this.$store.state.sysTestUser.currentPage,
                  "size": this.$store.state.sysTestUser.pageSize
                };
                Message.success(res.data.msg);
                // 刷新
                getSysTestUsers(params).then((res) => {
                  this.$store.state.sysTestUser.tableData = res.data.data;
                  this.$store.state.sysTestUser.total = res.data.total;
                })
              })
            }
        } else {
          console.log('error submit!!');
          return false;
        }
        });
      }
    }
  }
</script>

<style scoped>

</style>